//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using OpenLS.Spreadsheet.Internals;

namespace OpenLS.Spreadsheet
{
    partial class Worksheet
    {
        private DimensionCollection<T> getOwnerDimension<T>() where T : Dimension
        {
            if (typeof(T) == typeof(Column))
                return (DimensionCollection<T>)(object)Columns;
            return (DimensionCollection<T>)(object)Rows;
        }
        internal void InsertExitingDimensions<T>(int firstIndex, List<T> deletedDimensions) where T : Dimension
        {
            using (new StableSelectionScope(this))
            {
                DimensionCollection<T> list = getOwnerDimension<T>();
                if (Workbook.UndoEnabled)
                    foreach (T c in deletedDimensions)
                        Workbook.UndoManager.Add(new InsertDimensionUndoUnit<T>(list, c.Index));
                using (new TemporaryMovedCells(this, Utilities.GetGreaterThanOrEqualPredicate<T>(firstIndex)))
                {
                    list.OffsetItems(firstIndex, deletedDimensions.Count);
                    bool isColumn = typeof(T) == typeof(Column);
                    foreach (T dimension in deletedDimensions)
                    {
                        list.ReForce(dimension);
                        foreach (Cell cell in dimension.GetNonEmptyCells())
                        {
                            Cells.AddNonVolatileCell(cell);
                            if (isColumn)
                                Utilities.AddCellInRows(cell);
                            else
                                Utilities.AddCellInColumns(cell);
                        }
                    }

                    foreach (T dimension in deletedDimensions)
                        list.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, dimension, dimension.Index);
                    list.RaiseCollectionChanged(NotifyCollectionChangedAction.Reset);
                    list.TruncateOffsets(firstIndex, null);
                    //\\Cells.CollectionChanged
                }
            }
            
        }

        internal void DeleteExistingDimension<T>(T dimension) where T : Dimension
        {
            using (new StableSelectionScope(this))
            {
                DimensionCollection<T> dims = getOwnerDimension<T>();
                using (new TemporaryMovedCells(this, Utilities.GetGreaterThanPredicate<T>(dimension.Index)))
                {
                    dims.Remove(dimension);
                    List<Cell> removedCells = new List<Cell>();
                    foreach (Cell cell in dimension.GetNonEmptyCells())
                    {
                        if (typeof(T) == typeof(Row))
                            removeCellFromColumns(cell);
                        else
                            removeCellFromRows(cell);
                        removedCells.Add(cell);
                        Cells.RemoveCellNoEvents(cell);
                    }
                    foreach (var cell in removedCells)
                        OnCellRemoved(cell);
                    dims.RaiseCollectionChanged(NotifyCollectionChangedAction.Reset);
                    dims.TruncateOffsets(dimension.Index, null);
                }
                raiseLayoutChanged();
            }
            
        }


    }
}
